package mathsys;

import java.io.*;
import java.util.*;

public class MathExerciseGeneratorV1 {
    // 生成指定范围内的随机整数
    private static int getRandomNumber(int min, int max) {
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }

    // 生成加减法算式
    private static String generateExercise() {
        int num1 = getRandomNumber(0, 100);
        int num2 = getRandomNumber(0, 100);
        // 随机决定是加法还是减法(0表示加法，1表示减法)
        int operator = getRandomNumber(0, 1);

        // 处理减法情况，确保结果非负
        if (operator == 1) {
            // 如果第一个数小于第二个数，交换它们
            if (num1 < num2) {
                int temp = num1;
                num1 = num2;
                num2 = temp;
            }
            return num1 + " - " + num2 + " = ";
        } else {
            // 加法确保结果不超过100
            if (num1 + num2 > 100) {
                // 重新生成符合条件的加法题
                return generateExercise();
            }
            return num1 + " + " + num2 + " = ";
        }
    }

    // 生成HTML文件
    private static void generateHTML(List<String> exercises) {
        try (PrintWriter out = new PrintWriter(new FileWriter("math_exercises.html"))) {
            out.println("<!DOCTYPE html>");
            out.println("<html lang=\"zh-CN\">");
            out.println("<head>");
            out.println("    <meta charset=\"UTF-8\">");
            out.println("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
            out.println("    <title>加减法口算练习系统-v0.1</title>");
            out.println("    <style>");
            out.println("        body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }");
            out.println("        h1 { color: #333; text-align: center; }");
            out.println("        .exercise-container { margin-top: 30px; }");
            out.println("        .exercise { font-size: 18px; margin: 10px 0; padding: 8px; border-bottom: 1px solid #eee; }");
            out.println("        .column { float: left; width: 50%; }");
            out.println("        @media (max-width: 600px) {");
            out.println("            .column { width: 100%; }");
            out.println("        }");
            out.println("    </style>");
            out.println("</head>");
            out.println("<body>");
            out.println("    <h1>加减法口算练习系统-v0.1</h1>");
            out.println("    <div class=\"exercise-container\">");

            // 分两列显示题目
            out.println("        <div class=\"column\">");
            for (int i = 0; i < exercises.size() / 2; i++) {
                out.println("            <div class=\"exercise\">" + (i + 1) + ". " + exercises.get(i) + "</div>");
            }
            out.println("        </div>");

            out.println("        <div class=\"column\">");
            for (int i = exercises.size() / 2; i < exercises.size(); i++) {
                out.println("            <div class=\"exercise\">" + (i + 1) + ". " + exercises.get(i) + "</div>");
            }
            out.println("        </div>");

            out.println("    </div>");
            out.println("</body>");
            out.println("</html>");

            System.out.println("习题已生成，请打开 math_exercises.html 文件查看");
        } catch (IOException e) {
            System.err.println("生成HTML文件时出错: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        List<String> exercises = new ArrayList<>();

        // 生成50道习题
        for (int i = 0; i < 50; i++) {
            exercises.add(generateExercise());
        }

        // 生成HTML页面
        generateHTML(exercises);
    }
}
